package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdPatronsData;
import com.yanqu.road.entity.activity.stronghold.StrongholdWarUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_SET_WILD_PATRONS, desc = "派遣门客")
public class StrongholdWildDispatchCmd extends AbstractCommand {
    private int patronsId;
    private int wildId;
    int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.DispatcherPatronsReqMsg req = StrongholdWarProto.DispatcherPatronsReqMsg.parseFrom(packet.getBytes());
        patronsId = req.getPatronsId();
        wildId = req.getWildId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 是否有联盟
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            sendToPlayer(player);
            return;
        }

        if (!StrongholdActivityMgr.isWildTime()) {
            ret = GameErrorCode.E_STRONGHOLD_POWER_NOT_ENOUHT;
            sendToPlayer(player);
            return;
        }

        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_STRONGHOLD_NO_UNION;
            sendToPlayer(player);
            return;
        }


        activityId = activityInfo.getActivityId();
        //是否有门客
        UserPatrons patrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
        if (patrons == null) {
            ret = GameErrorCode.E_PATRONS_NO_FOUND;
            sendToPlayer(player);
            return;
        }
        //是否派遣过门客
        StrongholdModule strongholdModule = player.getModule(StrongholdModule.class);
        StrongholdWarUserData warData = player.getModule(StrongholdModule.class).getStrongholdWarData(activityId);
        player.getModule(StrongholdModule.class).calcPower(warData);
       /* if (warData.getWildPatronsId() != patronsId && strongholdModule.containDispatchPatrons(activityInfo.getActivityId(), patronsId)) {
            ret = GameErrorCode.E_STRONGHOLD_DISPATCH_ALREADY;
            sendToPlayer(player);
            return;
        }*/
        strongholdModule.resetBattlePatrons();
        String consume = null;
        Map<Integer, StrongholdPatronsData> dispatchMap = strongholdModule.getDispatchMap(activityInfo.getActivityId());
        if (dispatchMap != null) {
            StrongholdPatronsData data = dispatchMap.get(patronsId);
            if (data != null) {
                if (data.getBuyTimes() >= StrongholdActivityMgr.getWildBuyTimes()) {
                    ret = GameErrorCode.E_STRONGHOLD_DISPATCH_ALREADY;
                    sendToPlayer(player);
                    return;
                }

                if (data.getFreeTimes() >= StrongholdActivityMgr.getPatronsFreeTimes(player, patronsId)) {
                    consume = StrongholdActivityMgr.getWildDispatchConsume(data.getBuyTimes());
                    if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(consume))) {
                        ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                        sendToPlayer(player);
                        return;
                    }
                }
            }
        }

        int wildPatronsId = warData.getWildPatronsId();
        if (wildPatronsId != 0) {
            if (consume != null) {
                player.getModule(PreReduceModule.class).preReduce(consume);
            }
            //先算好门客信息
            //已经派遣过了， 去跨服校验
            sendToCross(player, patrons);
            return;
        }
        //未派遣， 设置派遣

        warData.setWildPatronsId(patronsId);
        if (wildId > 0) {
            if (consume != null) {
                player.getModule(PreReduceModule.class).preReduce(consume);
            }
            sendToCross(player, patrons);
            warData.setEnterWild(true);
        } else {
            sendToPlayer(player);
        }
    }

    private void sendToPlayer(GamePlayer player) {
        StrongholdWarProto.DispatcherPatronsRespMsg.Builder resp = StrongholdWarProto.DispatcherPatronsRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_SET_WILD_PATRONS, resp);
    }

    public void sendToCross(GamePlayer player, UserPatrons patrons) {
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        activityId = activityInfo.getActivityId();
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
        UserPatrons clonePatrons = patrons.cloneUserPatrons();
        long otherAddition = StrongholdWarMgr.getOtherAddition(player);
        BigInteger totalAbility = StrongholdWarMgr.calcStrongholdPatronsTotalAbility(clonePatrons, player.getModule(PatronsModule.class).getUserPatronsMap(), otherAddition);
        clonePatrons.setCurrentBlood(totalAbility);
        clonePatrons.setTotalBlood(totalAbility);
        StrongholdWarProto.DispatchPatronsToCrossWildReqMsg.Builder msg = StrongholdWarProto.DispatchPatronsToCrossWildReqMsg.newBuilder();
        msg.setWildId(wildId);
        msg.setPatronsStr(JSON.toJSONString(clonePatrons));
        msg.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.setActivityId(activityId);
        player.sendUnionActivityGroupPacket(Protocol.C_CROSS_STRONGHOLD_WAR_DISPATCH_WILD_PATRONS, msg, msg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
